//
//  QiMingAlertView.m
//  QiMingADXSDKDemo
//
//  Created by Leaves on 2025/1/2.
//

#import "QiMingPrivacyAlertView.h"

static NSString *privacyUrl = @"https://yourongsj.feishu.cn/wiki/AQIvwaOAFiDlYhkZgGAcZMmknqg";

@interface QiMingPrivacyAlertView () <UITextViewDelegate>

@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) UIButton *confirmBtn;

@end

@implementation QiMingPrivacyAlertView

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.frame= [UIScreen mainScreen].bounds;
    self.backgroundColor= [[UIColor blackColor] colorWithAlphaComponent:0.1];
    
    CGFloat alertW = [UIScreen mainScreen].bounds.size.width - 80;
    CGFloat btnW = (alertW - 20*3)/2;
    
    self.containerView.frame = CGRectMake(0, 0, alertW, 300);
    self.containerView.layer.position = self.center;
    
    [self addSubview:self.containerView];
    [self.containerView addSubview:self.titleLabel];
    [self.containerView addSubview:self.textView];
    [self.containerView addSubview:self.cancelBtn];
    [self.containerView addSubview:self.confirmBtn];
    
    self.titleLabel.frame = CGRectMake(0, 0, alertW, 50);
    self.textView.frame = CGRectMake(20, CGRectGetMaxY(self.titleLabel.frame), alertW - 20*2, 200);
    self.cancelBtn.frame = CGRectMake(20, CGRectGetMaxY(self.textView.frame), btnW, 40);
    self.confirmBtn.frame = CGRectMake(CGRectGetMaxX(self.cancelBtn.frame)+20, CGRectGetMaxY(self.textView.frame), btnW, 40);
    
    NSString *privacyPolicyString = @"亲爱的用户，欢迎您信任并使用启明广告服务，我们依据相关法律制定了《隐私协议》帮您了解我们手机，使用，存储和共享个人信息情况，请你在点击之前仔细阅读并理解相关条款。\n1、在使用我们的产品和服务时，将会提供与具体功能有关的个人信息（可能包括身份验证，位置信息，设备信息和操作日志等）\n2、我们会采用业界领先的安全技术来保护你的个人隐私，未经授权许可我们不会将上述信息共享给任何第三方或用于未授权的其他用途。\n如您同意请点击同意按钮并继续。";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:privacyPolicyString];
    // 查找“隐私协议”的范围
    NSRange privacyPolicyRange = [privacyPolicyString rangeOfString:@"隐私协议"];
    
    // 应用富文本属性
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:privacyPolicyRange];
    [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:privacyPolicyRange];
    [attributedString addAttribute:NSLinkAttributeName value:privacyUrl range:privacyPolicyRange];

    [self.textView setAttributedText:attributedString];
}

- (void)show {
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self animateIn];
}


- (void)animateIn {
    self.containerView.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView animateWithDuration: 0.4 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.05 options:0 animations:^{
        self.backgroundColor= [[UIColor blackColor] colorWithAlphaComponent:0.3];
        self.containerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)animateOut {
    [UIView animateWithDuration:0.3 animations:^{
        self.containerView.transform = CGAffineTransformMakeScale(0.1,0.1);
        self.alpha = 0.1;
    } completion:^(BOOL finished) {
        [self.containerView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    
    if (self.privacyBlock) {
        self.privacyBlock(privacyUrl);
    }
    
    return NO;
}

#pragma mark - Action
- (void)confirmAction {
    [self animateOut];
    if (self.confirmBlock) {
        self.confirmBlock();
    }
}

- (void)cancelAction {
    [self animateOut];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = [UIColor whiteColor];
        _containerView.layer.cornerRadius = 5;
        _containerView.layer.masksToBounds = YES;
    }
    return _containerView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"温馨提示";
    }
    return _titleLabel;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.editable = NO;
        _textView.dataDetectorTypes = UIDataDetectorTypeLink;
        _textView.delegate = self;
        _textView.backgroundColor = [UIColor clearColor];
    }
    return _textView;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc] init];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_cancelBtn setTitle:@"不同意" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.layer.cornerRadius = 5;
        _cancelBtn.layer.masksToBounds = YES;
    }
    return _cancelBtn;
}

- (UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [[UIButton alloc] init];
        _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_confirmBtn setTitle:@"同意并继续" forState:UIControlStateNormal];
        [_confirmBtn setBackgroundColor:[UIColor colorWithRed:36/255.0 green:132/255.0 blue:207/255.0 alpha:1]];
        [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
        _confirmBtn.layer.cornerRadius = 5;
        _confirmBtn.layer.masksToBounds = YES;
    }
    return _confirmBtn;
}

@end
